package com.awifi.demo.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @description: ToDo
 * @author: marin
 * @time: 2020/8/3 11:01
 */
@Slf4j
@Component
public class RedisUtil {

    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 功能描述: 根据key获取value
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 15:43
     */
    public Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    /**
     * 功能描述: 设置key和value，默认永久
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 16:07
     */
    public void set(String key,Object object) {
        redisTemplate.opsForValue().set(key, object);
    }

    /**
     * 功能描述:判断key是否存在
     * @Param:
     * @Return: flag
     * @Author: marin
     * @Date: 2020/8/4 13:40
     */
    public Boolean hasKey(String key) {
        Boolean flag = null;
        try {
            flag = redisTemplate.hasKey(key);
        }catch (Exception e) {
            log.error(key,e.getMessage());
            flag = false;
        }
        return flag;
    }

   /**
    * 功能描述: 设置value和过期时间
    * @Param:
    * @Return:
    * @Author: marin
    * @Date: 2020/8/4 11:49
    */
    public void set(String name,Object value,Long expires) {
        redisTemplate.opsForValue().set(name,value, expires,TimeUnit.SECONDS);
    }

    /**
     * 功能描述: 设置过期时间和过期时间单位
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 16:01
     */
    public void set(String name,Object value,Long expires,TimeUnit timeUnit) {
        redisTemplate.boundValueOps(name).set(value,expires,timeUnit);
    }

    /**
     * 功能描述: 获取key过期时间
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 13:40
     */
    public long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    /**
     * 功能描述: 设定key的失效时间，默认单位：秒
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 14:13
     */
    public boolean expire(String key,Long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            log.error(key, e);
            return false;
        }
    }

    /**
     * 功能描述: 按照时间单位设定key的过期时间
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 14:16
     */
    public boolean expire(String key,Long time,TimeUnit timeUnit) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, timeUnit);
            }
            return true;
        } catch (Exception e) {
            log.error(key, e);
            return false;
        }
    }

    /**
     * 功能描述: 单个、批量删除key与value
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 13:39
     */
    @SuppressWarnings("unchecked")
    public void delete(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete(key[0]);
            } else {
                redisTemplate.delete(CollectionUtils.arrayToList(key));
            }
        }
    }

    /**
     * 功能描述: 根据key按值递增
     * @Param: key
     * @Return: long
     * @Author: marin
     * @Date: 2020/8/4 11:43
     */
    public long incr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递增因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /**
     * 功能描述: 根据key按值递减
     * @Param: 关键字key 和 递减值delta
     * @Return: long类型
     * @Author: marin
     * @Date: 2020/8/4 13:57
     */
    public long decr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递减因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, -delta);
    }

    /**
     * 功能描述: hash类型按key获取值
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 14:23
     */
    public Object hget(String key, String hkey) {
        return redisTemplate.opsForHash().get(key, hkey);
    }

    /**
     * 功能描述:设置hash类型的值
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 14:36
     */
    public Boolean hset(String key,String hkey,String value) {
        try {
            redisTemplate.opsForHash().put(key, hkey, value);
            return true;
        } catch (Exception e) {
            log.error(key, e);
            return false;
        }
    }

    /**
     * 功能描述: 根据key和hashKey来删除
     * @Param:
     * @Return: 
     * @Author: marin
     * @Date: 2020/8/4 15:30
     */
    public void hdel(String key, Object... hkeys) {
        redisTemplate.opsForHash().delete(key, hkeys);
    }

    /**
     * 功能描述: 判断hash中是否存在key
     * @Param:
     * @Return: 
     * @Author: marin
     * @Date: 2020/8/4 15:45
     */
    public boolean hHasKey(String key, String item) {
        return redisTemplate.opsForHash().hasKey(key, item);
    }

    /**
     * 功能描述: hash指定数值增加
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 15:55
     */
    public double hincr(String key, String hkey, double by) {
        return redisTemplate.opsForHash().increment(key, hkey, by);
    }

    /**
     * 功能描述: 获取指定key对应的hash值
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:35
     */
    public Map<Object, Object> hGetAll(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 功能描述: hash指定数值递减
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 15:58
     */
    public double hdecr(String key, String hkey, double by) {
        return redisTemplate.opsForHash().increment(key, hkey,-by);
    }

    /**
     * 功能描述: 根据key获取set的值
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 16:33
     */
    public Set<Object> sGet(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            log.error(key, e);
            return null;
        }
    }

    /**
     * 功能描述: 设置Set的值
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 18:32
     */
    public long sSet(String key, Object... values) {
        try {
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            log.error(key, e);
            return 0;
        }
    }

    /**
     * 功能描述: 设置set的值和value，以及存活时间
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 18:49
     */
    public long sSetAndTime(String key, long time, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().add(key, values);
            if (time > 0)
                expire(key, time);
            return count;
        } catch (Exception e) {
            log.error(key, e);
            return 0;
        }
    }

    /**
     * 功能描述: 移除set中对应的value
     * @Param:
     * @Return: 
     * @Author: marin
     * @Date: 2020/8/4 19:12
     */
    public long setRemove(String key, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().remove(key, values);
            return count;
        } catch (Exception e) {
            log.error(key, e);
            return 0;
        }
    }

    /**
     * 功能描述: 设置ZSet的值和score
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 19:50
     */
    public Boolean zSSet(String key,Object value,double score) {
        try {
            return redisTemplate.opsForZSet().add(key, value, score);
        } catch(Exception e) {
            log.error(key,e);
            return false;
        }

    }

    /**
     * 功能描述: 根据key获取所有的zset值
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 19:50
     */
    public Set<Object> zSGet(String key) {
        try {
            return redisTemplate.opsForZSet().range(key,0,-1);
        } catch (Exception e) {
            log.error(key, e);
            return null;
        }
    }

    /**
     * 功能描述: 判断zset中是否存在 Key
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 19:55
     */
    public boolean zSHasKey(String key, Object value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            log.error(key, e);
            return false;
        }
    }


    /**
     * 功能描述:
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:06
     */
    public long zSSetAndTime(String key, long time, Set values) {
        try {
            Long count = redisTemplate.opsForZSet().add(key, values);
            if (time > 0)
                expire(key, time);
            return count;
        } catch (Exception e) {
            log.error(key, e);
            return 0;
        }
    }

    /**
     * 功能描述: 移除zset对应key的指定值
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:18
     */
    public long zSetRemove(String key, Object... values) {
        try {
            Long count = redisTemplate.opsForZSet().remove(key, values);
            return count;
        } catch (Exception e) {
            log.error(key, e);
            return 0;
        }
    }

    /**
     * 功能描述: 获取key对应的score
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:22
     */
    public Double zscore(String key, Object value) {
        try {
            Double score = redisTemplate.opsForZSet().score(key, value);
            return score;
        } catch (Exception e) {
            log.error(key, e);
            return 0.0;
        }
    }

    /**
     * 功能描述: 根据score区域值获取对应set
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:25
     */
    public Set<String> zrangeByScore(String key, double minScore, double maxScore) {
        try {
            Set sets = redisTemplate.opsForZSet().rangeByScore(key, minScore, maxScore);
            return sets;
        } catch (Exception e) {
            log.error(key,e);
            return null;
        }
    }

    /**
     * 功能描述: 根据key获取对应key的list全集
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:33
     */
    public List<Object> lGet(String key) {
        try {
            return redisTemplate.opsForList().range(key, 0, -1);
        } catch (Exception e) {
            log.error(key, e);
            return null;
        }
    }

    /**
     * 功能描述: 根据key获取指定数量的list
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:33
     */
    public List<Object> lGet(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            log.error(key, e);
            return null;
        }
    }

    /**
     * 功能描述: leftpush到list
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:39
     */
    public long lpush(String key,Object value) {
        try {
            return redisTemplate.opsForList().leftPush(key,value);
        } catch (Exception e) {
            log.error(key,e);
            return 0;
        }
    }

    /**
     * 功能描述: leftPush 批量添加数据
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/6 10:22
     */
    public long lpush(String key,List<Object> values) {
        try {
            Long aLong = redisTemplate.opsForList().leftPushAll(key, values);
            return aLong;
        } catch (Exception e) {
            log.error(key,e);
            return 0;
        }
    }

    /**
     * 功能描述: leftpush String类型的值
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/6 15:08
     */
    public boolean lpushString(String key,String value) {
        try {
            Long aLong = stringRedisTemplate.opsForList().leftPush(key, value);
            return true;
        } catch (Exception e) {
            log.error(key,e);
            return false;
        }
    }

    /**
     * 功能描述: rightPush到list
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:42
     */
    public long rpush(String key, String value) {
        try {
            return redisTemplate.opsForList().rightPush(key,value);
        } catch (Exception e) {
            log.error(key,e);
            return 0;
        }
    }

    /**
     * 功能描述: leftpush指定key的值和存活时间
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:45
     */
    public boolean lpush(String key, Object value, long time) {
        try {
            redisTemplate.opsForList().leftPush(key, value);
            if (time > 0)
                expire(key, time);
            return true;
        } catch (Exception e) {
            log.error(key, e);
            return false;
        }
    }

    /**
     * 功能描述: rightpush指定key的值和存活时间
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:45
     */
    public boolean rpush(String key, Object value, long time) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            if (time > 0)
                expire(key, time);
            return true;
        } catch (Exception e) {
            log.error(key, e);
            return false;
        }
    }

    /**
     * 功能描述: rightPush整个list
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:49
     */
    public boolean rpush(String key, List<Object> value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            log.error(key, e);
            return false;
        }
    }

    /**
     * 功能描述: rightPush整个list并设置时间
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:49
     */
    public boolean rpush(String key, List<Object> value, long time) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            if (time > 0)
                expire(key, time);
            return true;
        } catch (Exception e) {
            log.error(key, e);
            return false;
        }
    }

    /**
     * 功能描述: 删除对应key指定个数的value
     * @Param:
     * @Return:
     * @Author: marin
     * @Date: 2020/8/4 20:50
     */
    public long lRemove(String key, long count, Object value) {
        try {
            Long remove = redisTemplate.opsForList().remove(key, count, value);
            return remove;
        } catch (Exception e) {
            log.error(key, e);
            return 0;
        }
    }

}
